//
// Created by bjliuyong on 2021/6/7.
//

#ifndef GC_MEMORY_CHUNK_H
#define GC_MEMORY_CHUNK_H

#include "memory_cell.h"
#include <list>

using namespace std;

class MemoryChunk {
private:
    /**
     * 创建Chunk的文件名
     */
    char *m_filename;

    /**
     * 创建Chunk的文件位置
     */
    uint m_line;

    /**
     * 该Chunk的内存大小
     */
    uint m_size;

    uint m_cell_num;

    uint m_used_cell_num;

    /**
     * Align size
     */
    uint m_align_size;

    list<MemoryCell *> m_available_table;
    list<MemoryCell *> m_used_table;
    list<MemoryCell *> m_transfer_table;

    pvoid m_addr;

    /**
     * 当前Cell的起始位置
     *      复制算法用
     */
    uint m_cell_start;

    /**
     * mark and copy
     */
    list<MemoryCell*> m_idle_table;

public:
    MemoryChunk(uint size, char *filename, uint line);

    ~MemoryChunk();

public:
    MemoryChunk *inc_used_cell_num(uint step);
    MemoryChunk *desc_used_cell_num(uint step);
    void set_used_cell_num(uint n);

    MemoryCell * malloc(uint size);

    MemoryCell * _malloc(MemoryCell *cell, uint cell_num);
    MemoryCell * real_malloc(uint size);
    MemoryCell * malloc_after_gc(MemoryCell *transferCell);

    void free_available_table();
    list<MemoryCell*> *get_available_table();
    void set_available_table(list<MemoryCell *> &table);

    void free_used_table();
    list<MemoryCell*> *get_used_table();
    list<MemoryCell*> *get_transfer_table();

    void print_available_table();
    void print_used_table();
    void print_all_table();

    pvoid get_addr();
    uint get_align_size();

    list<MemoryCell*> * get_idle_table();

    uint get_cell_num();
    uint  renew_cell_start();
    uint get_cell_start();
    uint get_new_cell_start();
};

#endif //GC_MEMORY_CHUNK_H
